<template>
    <div id="UploadAvatar">
        <ToolBar>
            修改头像
            <el-button type="primary" circle class="add" size='mini' @click="back">返回</el-button>
        </ToolBar>
        <el-upload
            class="avatar-uploader"
            action="123"
            :http-request="upLoad"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
    </div>
</template>

<script>
import ToolBar from '@/components/ToolBar.vue';
export default {
    components: {
        ToolBar
    },
    data() {
      return {
        imageUrl: ''
      };
    },
    methods:{
        back() {
            this.$router.push({ name: 'personalcenter' })
        },
        handleAvatarSuccess(res, file) {
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        upLoad(file) {
            var formData = new FormData();
            formData.append('file', file.file);
            // formData.file=file.file;
            // console.log(file.file);
            // formData
            console.log(formData)
            this.axios.post('/users/avatar',file.file).then((res) => {
                console.log(res);
            }).catch(() => { })
            // this.$http.uploadHttp('/udb/api/v1/oss/fileUpload', formData).then((res) => {
            //     console.log(res);
            //     console.log('上传成功');
            //     if (res.data.code === 0) {
            //     this.successUrl = res.data.response; // 请求之后将返回的URL给修改的接口在进行一次修改请求
            //     this.headUrl = res.data.response; // 请求成功之后赋给头像的URL
            //     this.$message('头像上传成功');
            //     this.$store.dispatch('person/setAvatar', this.successUrl);
            //     const obj = {
            //         headUrl: this.successUrl
            //     };
            //     this.edit(obj);
            //     // this.getAccount();
            //     } else {
            //     this.$message('头像上传失败');
            //     }
            // });
        },
    }
}
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
